package com.oscarmovie.action;

import java.util.List;
import java.util.Set;

import com.opensymphony.xwork2.ActionSupport;
import com.oscarmovie.Constants;
import com.oscarmovie.model.Audio;
import com.oscarmovie.model.Image;
import com.oscarmovie.model.Language;
import com.oscarmovie.model.Music;
import com.oscarmovie.service.LanguageService;
import com.oscarmovie.service.MusicService;

public class MusicAction extends ActionSupport {

	private static final long serialVersionUID = 1L;
	private MusicService musicService;
	private Music music;
	private List<Music> musics;
	private LanguageService languageService;
	private List<Language> languages;
	private String audiosData;
	public String execute() throws Exception {
		throw new Exception("Please specify method name!");
	}
	public String add() throws Exception {
		setMusic(null);
		audiosData ="";
		return Constants.ADD;
	}

	public String edit() throws Exception {
		loadAll();
		load();
		audiosData ="";
		Set<Audio> audios = music.getAudios();
		if(audios!= null){
			for(Audio audio: audios){
				audiosData += audio.getId() + ",";
			}
		}
		return Constants.EDIT;
	}
	public String save() throws Exception {
		String audioIds[] = audiosData.split(",");
		if (!"".equals(audiosData)) {
			for (String id : audioIds) {
				Audio audio = new Audio();
				audio.setId(Integer.parseInt(id));
				music.getAudios().add(audio);
			}
		}
		musicService.save(music);
		return edit();
	}

	public String load() throws Exception {
		this.setMusic(musicService.load(music.getId()));
		return SUCCESS;
	}

	

	public String loadAll() throws Exception {
		this.musics = musicService.loadAll();
		this.languages = languageService.loadAll();
		return SUCCESS;
	}

	public String select() throws Exception {
		loadAll();
		return Constants.SELECT;
	}

	public List<Music> getMusics() {
		return musics;
	}

	public void setMusicService(MusicService musicService) {
		this.musicService = musicService;
	}

	public void setMusic(Music music) {
		this.music = music;
	}

	public Music getMusic() {
		return music;
	}

	public void setLanguageService(LanguageService languageService) {
		this.languageService = languageService;
	}

	public LanguageService getLanguageService() {
		return languageService;
	}

	public void setLanguages(List<Language> languages) {
		this.languages = languages;
	}

	public List<Language> getLanguages() {
		return languages;
	}
	public String getAudiosData() {
		return audiosData;
	}
	public void setAudiosData(String audiosData) {
		this.audiosData = audiosData;
	}
	

}
